package com.yc.navy.springmvc.controller;

import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yc.navy.springmvc.bean.MemberInfo;
import com.yc.navy.springmvc.dto.OrderDetailDTO;
import com.yc.navy.springmvc.enums.ResultEnum;
import com.yc.navy.springmvc.service.IOrderInfoService;
import com.yc.navy.springmvc.util.ConstantIfo;
import com.yc.navy.springmvc.vo.ResultVO;



@RestController
@RequestMapping("/order")
public class OrderInfoController {
	@Autowired
	private IOrderInfoService orderInfoService;
	
	/**
	 * 查询当前登录用户的购物车信息
	 * @param session
	 * @return
	 */
	
	private Integer getMno(HttpSession session) {
		Object obj = session.getAttribute(ConstantIfo.CURRENTLOGINMEMBER);
		if(obj == null) {
			return null;
		}
		MemberInfo mf = (MemberInfo) obj;
		return mf.getMno();
	}
	
	@PostMapping("/add")
	public ResultVO add(@RequestParam("cnos[]") String[] cnos, String ano, HttpSession session) {
		Integer mno = this.getMno(session);
		if(mno == null) {
			return new ResultVO(ResultEnum.LOGIN_ERROR);
		}
		
		try {
			Map<String, Object> map = orderInfoService.add(cnos, ano, mno);
			System.out.println(map);
			if(map != null && !map.isEmpty()) {
				return new ResultVO(ResultEnum.SUCCESS, map);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ResultVO(ResultEnum.ERROP);	
	}
	
	@GetMapping("/list")
	public ResultVO list (HttpSession session) {
		Integer mno = this.getMno(session);
		if(mno == null) {
			return new ResultVO(ResultEnum.LOGIN_ERROR);
		}
		List<OrderDetailDTO> list = orderInfoService.list(mno);
		if(list != null && !list.isEmpty()) {
			return new ResultVO(ResultEnum.SUCCESS, list);
		}
		return new ResultVO(ResultEnum.ERROP);
			
	}
	
	@PostMapping("/update")
	public ResultVO update(@RequestParam Map<String, Object> map) {
		int result = orderInfoService.update(map);
		if(result > 0 ) {
			return new ResultVO(ResultEnum.SUCCESS);
		}
		return new ResultVO(ResultEnum.ERROP);	
	}
}
